package com.situ.hospital.dao.Impl;

import com.situ.hospital.dao.IPatientDAO;
import com.situ.hospital.pojo.Patient;
import com.situ.hospital.pojo.query.PatientQuery;
import com.situ.hospital.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class PatientDAOImpl implements IPatientDAO {

    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public List<Patient> selectByPage(PatientQuery patientQuery) {
        String sql = "SELECT id,`name`,age,gender,create_time,update_time FROM patient WHERE 1=1";
        List<Object> queryList = new ArrayList<>();
        String queryName = patientQuery.getName();
        if(queryName != null && !"".equals(queryName)) {
            sql += " AND `name` LIKE ?";
            queryList.add("%" + queryName + "%");
        }
        String queryGender = patientQuery.getGender();
        if(queryGender != null && !"".equals(queryGender)) {
            sql += " AND gender LIKE ?";
            queryList.add(queryGender);
        }
        Date beginCreateTime = patientQuery.getBeginCreateTime();
        Date endCreateTime = patientQuery.getEndCreateTime();
        if(beginCreateTime != null && endCreateTime != null) {
            sql += " AND create_time BETWEEN ? AND ?";
            queryList.add(beginCreateTime);
            queryList.add(endCreateTime);
        }
        sql += " LIMIT ?,?";
        int offset = (patientQuery.getPage() - 1) * patientQuery.getLimit();
        queryList.add(offset);
        queryList.add(patientQuery.getLimit());
        return template.query(sql, new BeanPropertyRowMapper<>(Patient.class), queryList.toArray());
    }

    @Override
    public int selectTotalCount(PatientQuery patientQuery) {
        String sql = "SELECT COUNT(*) FROM patient WHERE 1=1";
        List<Object> queryList = new ArrayList<>();
        String queryName = patientQuery.getName();
        if(queryName != null && !"".equals(queryName)) {
            sql += " AND `name` LIKE ?";
            queryList.add("%" + queryName + "%");
        }
        String queryGender = patientQuery.getGender();
        if(queryGender != null && !"".equals(queryGender)) {
            sql += " AND gender LIKE ?";
            queryList.add(queryGender);
        }
        Date beginCreateTime = patientQuery.getBeginCreateTime();
        Date endCreateTime = patientQuery.getEndCreateTime();
        if(beginCreateTime != null && endCreateTime != null) {
            sql += " AND create_time BETWEEN ? AND ?";
            queryList.add(beginCreateTime);
            queryList.add(endCreateTime);
        }
        return template.queryForObject(sql, Integer.class, queryList.toArray());
    }

    @Override
    public void deleteById(int id) {
        String sql = "DELETE FROM patient WHERE id=?";
        template.update(sql, id);
    }

    @Override
    public void add(Patient patient) {
        String sql = "INSERT INTO patient(`name`,age,gender) VALUES(?,?,?)";
        template.update(sql, patient.getName(), patient.getAge(), patient.getGender());
    }

    @Override
    public Patient selectById(int id) {
        String sql = "SELECT id,`name`,age,gender,create_time,update_time FROM patient WHERE id=?";
        return template.queryForObject(sql, new BeanPropertyRowMapper<>(Patient.class), id);
    }

    @Override
    public void update(Patient patient) {
        String sql = "UPDATE patient SET `name`=?,age=?,gender=? WHERE id=?";
        template.update(sql, patient.getName(), patient.getAge(), patient.getGender(), patient.getId());
    }

    @Override
    public List<Patient> selectAll() {
        String sql = "SELECT id,`name`,age,gender,create_time,update_time FROM patient";
        return template.query(sql, new BeanPropertyRowMapper<>(Patient.class));
    }
}
